#include "stdafx.h"

#include "ArrayPool.h"
#include <assert.h>



ArrayPool::ArrayPool()
{
    assert(_instance == nullptr);

    _instance = this;
}

uint32_t ArrayPool::add(const QStringList &obj, const QString &tabStr)
{
    return instance()->_add(obj, tabStr);
}

Array ArrayPool::at(uint32_t id)
{
    return instance()->_at(id);
}

void ArrayPool::free(uint32_t id)
{
    instance()->_free(id);
}

uint32_t ArrayPool::_add(const QStringList &strlist, const QString &tabStr)
{
    //QStringList strlist;
    //for (const QVariant &v : obj)
    //{
    //    strlist.push_back(v.toString());
    //}
    std::lock_guard<std::mutex> lock(locker);
    arrays.emplace(lastIndex, Array{ std::move(strlist), tabStr });
    return lastIndex++;
}

Array ArrayPool::_at(uint32_t id)
{
    return arrays.at(id);
}

void ArrayPool::_free(uint32_t id)
{
    std::lock_guard<std::mutex> lock(locker);
    arrays.erase(id);
}

ArrayPool * ArrayPool::instance()
{
    if (_instance == nullptr)
        _instance = new ArrayPool;
    return _instance;
}

ArrayPool * ArrayPool::_instance = nullptr;
